#include "union_find.h"
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <input_file>" << std::endl;
        return 1;
    }

    std::ifstream inFile(argv[1]);
    if (!inFile) {
        std::cerr << "Error: Could not open file " << argv[1] << std::endl;
        return 1;
    }

    int N;
    inFile >> N;
    
    UF uf(N);
    
    int p, q;
    while (inFile >> p >> q) {
        if (!uf.connected(p, q)) {
            uf.Union(p, q);
            std::cout << p << " " << q << std::endl;
        }
    }

    inFile.close();
    return 0;
}
